package com.gzp.config;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gzp.service.UserService;
import com.gzp.service.impl.OrderServiceImpl;

/**
 * dubbo使用配置类配置
 * 
 * @author gzp
 *
 */
@Configuration
public class DubboConfig {

	/**
	 * 声明当前提供方应用程序的名称
	 * 
	 * @return
	 */
	@Bean
	public ApplicationConfig applicationConfig() {
		return new ApplicationConfig("my-config-consumer");
	}

	/**
	 * 指定注册中心地址
	 * 
	 * @return
	 */
	@Bean
	public RegistryConfig registryConfig() {
		return new RegistryConfig("zookeeper://127.0.0.1:2181");
	}

	/**
	 * 引用UserService服务
	 * 
	 * @param orderServiceImpl
	 * @param applicationConfig
	 * @param registryConfig
	 * @return
	 */
	@Bean
	public ReferenceConfig<UserService> referenceConfigUserService(OrderServiceImpl orderServiceImpl,
			ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
		// 注入UserService服务
		ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
		referenceConfig.setInterface(UserService.class);
		referenceConfig.setId("userService");
		// 注入应用程序名称
		referenceConfig.setApplication(applicationConfig);
		// 注入注册中心配置
		referenceConfig.setRegistry(registryConfig);
		// 给OrderServiceImpl注入UserService
		orderServiceImpl.setUserService(referenceConfig.get());
		return referenceConfig;
	}
}
